﻿
#include <QDebug>

#include "websocket/LiveManager.h"

#include "websocket/websocketmanager.h"
#include "base/TraceLog.h"
#include "IRtspReadManager.h"


LiveManager::LiveManager(WebSocketManager * _websocketmanager)
    :_manager(_websocketmanager)
    ,pull_channel_id(0)
{
    mrtsps.clear();

    event_queue = new EventListQueue();

    base::SimpleThread::Start();

    //connect(this, &LiveManager::closedPull, this, &LiveManager::DoclosedPull);
}
LiveManager::~LiveManager()
{

    base::SimpleThread::Stop();
    time_event.Set();
    base::SimpleThread::Join();

    QList<IRtspReadManager*>::iterator it;

    for(it=mrtsps.begin();it!=mrtsps.end();)
    {
        IRtspReadManager* tmp = *it;

        it=mrtsps.erase(it);

        tmp->StopRtspInput();

        delete tmp;
        tmp = NULL;
    }

    mrtsps.clear();

    event_queue->DoQueueQuit();
    delete event_queue;
}

int  LiveManager::DoStartPullStream(QString & pull_url,int num)
{
    QMutexLocker locker(&cs_live_);
    if(pull_url.indexOf("rtsp://") <0)
    {
        return -1;
    }

    if(num < 0)
    {
        return -1;
    }

    if(mrtsps.size() > 0)
    {
        //获取pull url 看看是不是一样的
        //查询同一个地址再次连接
        for ( int i=0; i!=mrtsps.size(); ++i )
        {
            //            std::string url = mlives.at(i)->GetPullUrl();
            //            if ( QString::fromStdString(url) == pull_url)
            //            {
            //                return RETURN_VALUE::SR_RETURN_TWICE;
            //            }

        }
    }

    LogDEBUG("coming a pull url %s start pull it num:%d",pull_url.toStdString().c_str(),num);

    for(int i = 0; i < num; i++)
    {
        IRtspReadManager *rtsp = new IRtspReadManager(this);

        assert(rtsp != NULL);

        if(rtsp->StartRtspInputUrl(pull_url.toStdString().c_str()) < 0)
        {
            LogERR("url %s StartRtspInputUrl failed",pull_url.toStdString().c_str());
            delete rtsp;
            rtsp = NULL;
            continue;
        }

        mrtsps << rtsp;

        pull_channel_id ++;
    }

    return 0;
}
int  LiveManager::DoStopPullStream(QString & pull_url)
{
    QMutexLocker locker(&cs_live_);

    if(pull_url.indexOf("rtsp://") <0)
    {
        LogDEBUG("remove pull url %s",pull_url.toStdString().c_str());
        return -1;
    }

    if(mrtsps.size() > 0)
    {
        //获取pull url 看看是不是一样的
        //查询同一个地址再次连接

        QList<IRtspReadManager*>::iterator it;

        for(it=mrtsps.begin();it!=mrtsps.end();)
        {
            IRtspReadManager* rtsp = *it;
            std::string url =rtsp->GetPullUrl();
            if ( QString::fromStdString(url) == pull_url)
            {
                it=mrtsps.erase(it);
                rtsp->StopRtspInput();
                delete rtsp;
                rtsp = NULL;
                pull_channel_id --;
            }
            else
            {
                ++it;    //指向下一个位置
            }
        }

        LogDEBUG("remove pull url %s",pull_url.toStdString().c_str());
        return 0;
    }

    return 0;
}

void LiveManager::Run()
{
    while(true)
    {
        if(base::SimpleThread::IsStop())
        {
            break;
        }

        time_event.TryWait(2000);

        LogDEBUG("Current number of pull rtsp %d ",pull_channel_id);

        EventList eventl;
        if(false == event_queue->deQueue(eventl, true))
        {
            continue;
        }

        if(eventl.type == CONNECTION_WORK_READ_ERROR)
        {
            QMutexLocker locker(&cs_live_);
            QList<IRtspReadManager*>::iterator it;
            for(it=mrtsps.begin();it!=mrtsps.end();)
            {
                IRtspReadManager* rtsp = *it;
                std::string url =rtsp->GetPullUrl();
                if ( rtsp == eventl.rtsp_this)
                {
                    it=mrtsps.erase(it);
                    rtsp->StopRtspInput();
                    delete rtsp;
                    rtsp = NULL;
                    pull_channel_id --;
                }
                else
                {
                    ++it;    //指向下一个位置
                }
            }
        }

    }

}

void LiveManager::OnReadErrorEvent(void * rtsp_this, std::string pull_url)
{
    EventList eventl;
    eventl.type = CONNECTION_WORK_READ_ERROR;
    eventl.rtsp_this = rtsp_this;

    event_queue->enQueue(eventl);
    time_event.Set();
}





